﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Antlr.StringTemplate;
using UCenter.Core.Common;

namespace UCenter.TemplateEngine
{
    public sealed class TemplateService
    {
        public static string Generate(TemplateContract contract)
        {
            // get template file path
            HttpContext context = HttpContext.Current;
            if (context == null)
                throw new Exception("Current httpcontext is null");
            string path = context.Server.MapPath(String.Format("~/App_Data/Templates/{0}", AppSettings.DefaultCulture));

            // create the template instance
            StringTemplateGroup templateGroup = new StringTemplateGroup("template", path);
            StringTemplate template = templateGroup.GetInstanceOf(contract.Name);

            foreach (var para in contract.Parameters)
            {
                template.SetAttribute(para.Key, para.Value);
            }

            string a = template.ToString();
            return template.ToString();
        }
    }
}
